package com.zcxy.edu.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.zcxy.common.config.RuoYiConfig;
import com.zcxy.common.core.redis.RedisCache;
import com.zcxy.common.utils.http.HttpUtilsNew;
import com.zcxy.edu.service.IEduTokenService;
import com.zcxy.system.domain.SysConfig;
import com.zcxy.system.service.ISysConfigService;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * token Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-20
 */
@Service
public class EduTokenServiceImpl implements IEduTokenService {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private RedisCache redisCache;

    private static final Logger log = LoggerFactory.getLogger(EduTokenServiceImpl.class);

    /**
     * 获取保存token
     * @return
     */
    public void saveToken(){
        String url = RuoYiConfig.getJwtTokenPath();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", RuoYiConfig.getJwtTokenAppid());
        jsonObject.put("secret", RuoYiConfig.getJwtTokenSecret());

        Map<String,String> map = new HashMap<String,String>();

        log.info("请求获取token地址:"+url);
        log.info("请求获取token参数:"+jsonObject.toJSONString());
        JSONObject result = HttpUtilsNew.sendPost(url,jsonObject,map);

        log.info("请求获取token返回结果:"+result.toJSONString());
        if(result.getInteger("status")!=200){
            return;
        }

        String token = result.getString("data");

        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("eduToken");
        List<SysConfig> list = sysConfigService.selectConfigList(sysConfig);
        if(list.size()==0){
            sysConfig.setConfigName("教育token");
            sysConfig.setConfigValue(token);
            sysConfig.setConfigType("Y");
            sysConfig.setCreateTime(new Date());
            sysConfigService.insertConfig(sysConfig);
        }else{
            sysConfig = list.get(0);
            sysConfig.setConfigValue(token);
            sysConfig.setUpdateTime(new Date());
            sysConfigService.updateConfig(sysConfig);
        }
        sysConfigService.resetConfigCache();

    }

    /**
     * 获取保存POA token
     * @return
     */
    public void savePOAToken(){
        String url = RuoYiConfig.getPOAPath();

        ArrayList<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("grant_type", "client_credentials"));
        list.add(new BasicNameValuePair("client_id", "qonJfouKzJ7f5cLVwVP2r6bNJ1c="));
        list.add(new BasicNameValuePair("scope", "messagecenter:v1:sendMessage"));
        list.add(new BasicNameValuePair("client_secret", "EHd8gb6bdYAgRZxBFoPqBL957O9AKR_y12Nn25Oy3eU="));

        log.info("请求获取token地址:"+url);
        log.info("请求获取token参数:"+list.toString());

        String data = HttpUtilsNew.postData(url, list, null);
        JSONObject result = JSONObject.parseObject(data);

        log.info("请求获取token返回结果:"+result.toJSONString());

        String token = result.getString("access_token");

        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("POAToken");
        List<SysConfig> listC = sysConfigService.selectConfigList(sysConfig);
        if(listC.size()==0){
            sysConfig.setConfigName("POAtoken");
            sysConfig.setConfigValue(token);
            sysConfig.setConfigType("Y");
            sysConfig.setCreateTime(new Date());
            sysConfigService.insertConfig(sysConfig);
        }else{
            sysConfig = listC.get(0);
            sysConfig.setConfigValue(token);
            sysConfig.setUpdateTime(new Date());
            sysConfigService.updateConfig(sysConfig);
        }
        sysConfigService.resetConfigCache();
    }

}
